<?php

class jihuaAction extends Action
{

    public function jihua()
    {

        $time = time();//当前时间

        $str = '';
        if (file_exists('jihua.txt')) {
            $jihuatime = file_get_contents('jihua.txt');


        } else {
            $jihuatime = $time;
        }

        $interval_time = $time - $jihuatime;//间隔时间，这个要跟计划任务的时间对准，最好是比计划任务少个几秒


        //一分钟内再次访问就无效
        file_put_contents('jihua.txt', $time);
        //获取所有的有效的推广计划
        $plan = M('plan')->where(['state' => 1])->getField('plan_id', true);
        echo M()->_sql();
        //获取所有的有效的推广组
        $group = M('group')->where(['state' => 1, 'plan_id' => array('in', $plan)])->getField('group_id', true);
        echo M()->_sql();
        //获取所有有效的创意条数
        $originality = M('originality')->where(['state' => 2, 'group_id' => array('in', $group)])->field('originality_id,member_id')->select();
        echo M()->_sql();
        $oidList = array_column($originality, 'originality_id');

        //获取在当前时间内执行的点击
        $where = [
            'originality_id' => ['in', $oidList],
            'start_date' => ['LT', $jihuatime + 60],
            'end_date' => ['GT', $time - 60] //结束时间往后预算一分钟，解决最后一次无法扣费的问题
        ];
        $data = M('data')->where($where)->select();
        echo M()->_sql();

        dump($data);
        //声明一个变量，放有效的origin
        $origin = [];

        //循环data的数据进行扣钱
        foreach ($data as $v) {
            //剔除开始时间大于当前时间的数据
                if($v['start_date']>$time){
                    echo '当前时间小于开始时间';
                    continue;
                }

            //结束时间减去开始时间，然后除以点击次数，就是每秒的点击量
            $origin[] = $v['originality_id'];
            $counttime = $v['end_date'] - $v['start_date'];//总时间


            $member_id = 0;
            //获取这个账号的余额，然后余额减去这个扣的钱数
            foreach ($originality as $or) {
                if ($v['originality_id'] == $or['originality_id']) {
                    $member_id = $or['member_id'];
                    break;
                }
            }
            if ($member_id == 0) {
                $str .= 'member_id 为0' . "\n";
                continue;
            }

            //最后一次扣费问题解决思路：对比当前时间，此创意单条结束时间和上次扣费时间，如果上次扣费时间大于单条结束时间，则跳过此次循环
            if ($jihuatime > $v['end_date']) {
                echo '上次扣费时间大于结束时间';
                continue;
            }

            dump($v['data_id']);
            $str .= "\n". '计划时间'.$jihuatime;
            dump($jihuatime);
            //开始时间
             $str .= "\n".'开始时间'.$v['start_date'];
            dump($v['start_date']);
            //结束时间
            $str .= "\n". '结束时间'.$v['end_date'];
            dump($v['end_date']);
            //当前时间
            $str .= "\n". '当前时间'.$time;
            dump($time);
            echo "<br>";
            //
            //如果单条结束时间大于上次扣费时间，但是小于当前时间，则只计算上次扣费时间到结束时间中间的点击量
            if ($jihuatime < $v['end_date'] && $time > $v['end_date']) {
                $interval_time = $v['end_date'] - $jihuatime;
                $str .= "\n". '解决最后一次扣费问题';
            }

            //解决第一次扣费问题，如果上次扣费时间小于开始时间，但是开始时间小于当前时间，则说明在上次扣费之后，这条才执行，则计算当前时间减去开始时间
            if ($jihuatime < $v['start_date'] && $time > $v['start_date']) {
                $interval_time = $time - $v['start_date'];
                $str .= "\n". '解决第一次扣费问题';
            }
            $str .= "\n". '间隔时间';
            $str .= "\n". '<br>';
            $str .= "\n". $interval_time;
            echo '<br>';
            dump($interval_time);
            //创意点击量跟展示量增加量写入originality表
            $djcs = $v['djcs'] / $counttime * $interval_time;  //计算这段时间点击量
            $zxcs = $v['zxcs'] / $counttime * $interval_time;//计算这段时间展示量
            $str .= "\n". '点击量$v[\'djcs\'] / $counttime * $interval_time='.$djcs;
            echo '<br>';
            $str .= "\n". '展示量'.$zxcs;
            $price = $djcs * $v['pjcb']; //扣的钱数是这段时间的点击次数乘以点击一次的费用
            $price = round($price, 2);
            $str .= '点击量：' . $djcs . "\n";
            $str .= '展示次数：' . $zxcs . "\n";

            $balance = M('account')->where(array('member_id' => $member_id))->order('account_id desc')->getfield('balance');
            if ($balance < 0 || $balance == 0) {
                $str .= 'balance为:' . $balance . "\n";
                continue;
            }

            $balance1 = $balance - $price;
            $str .= '$balance - $price:' . $balance . '-' . $price . "\n";

            if ($balance1 < 0) {
                $price = $balance;//如果这次扣的比账户上的余额多，则最多只能扣账户那么多的钱
                $balance = 0;
                $str .= 'price:' . $price . "\n";
                $str .= 'balance:' . $balance . "\n";
            } else {
                $balance = $balance1;
            }
            echo "<br>";
            $str .= "\n". '应扣钱数'.$price;

            //写入数据表中
            $account = array(
                'date' => $time,
                'version_id' => 0,
                'member_id' => $member_id,
                'balance' => $balance,
                'amount' => $price,
                'amount_type' => 2
            );


            //扣费
            M('account')->add($account);

            M('originality')->where(['originality_id' => $v['originality_id']])->setInc('djcs', $djcs);//点击次数
            M('originality')->where(['originality_id' => $v['originality_id']])->setInc('zxcs', $zxcs);//展现次数
            M('originality')->where(['originality_id' => $v['originality_id']])->setInc('countprice', $price);//总费用增加

        }

        $where2 = [
            'originality_id' => ['in', $origin]
        ];
        $data = M('originality')->where($where2)->select();
        //计算originality的一堆参数
        foreach ($data as $vv) {
            $origin_data = array(
                'qccb' => $vv['countprice'] / $vv['zxcs'] * 1000,//千次成本

                'djl' => $vv['djcs'] / $vv['zxcs'] * 100,//点击率

            );
            //更新originality的这些参数
            M('originality')->where(['originality_id' => $vv['originality_id']])->save($origin_data);
            //  echo M()->_sql();
        }

        file_put_contents('jihualog.txt', date("Y-m-d H:i:s", $time) . "\n", FILE_APPEND);
        file_put_contents('jihualog.txt', $str . "\n", FILE_APPEND);
        echo 'success';
    }

    //用户数据中心数据统计处理 一天一次
    public function updateDatedata()
    {
        $start_time = time();
        $date = strtotime(date('Y-m-d',$start_time));
        $end_time = $start_time - 60 * 60 * 24; // 昨天这个时候

        //获取用户列表
        $member_id_list = M('member')->getField('member_id', true);



        //获取这些用户的每个创意的点击数据，展现数据
        foreach ($member_id_list as $v) {

            //获取这个用户的每个创意的数据
            $origin = M('originality')->where(['member_id' => $v])->field('djcs,zxcs,originality_id')->select();
//获取上次统计时间
            $history1 = M('datedata')->where(['member_id'=>$v])->order('datedata_id desc')->limit(1)->getField('date');



            //获取这个用户的上次的创意的统计
            $datedata = M('datedata')->where(['member_id' => $v,'date'=>$history1])->field('djcs,zxcs,member_id,originality_id')->select();
//定义一个标志，此数据是否是新的
            foreach ($origin as $vv) {
                $isnew = 1;

                foreach ($datedata as $dv) {


                    if ($vv['originality_id'] == $dv['originality_id']) {

                        $isnew = 2;//不是新的
                        echo '不是新的';
                        dump($isnew);
                        //计算昨天的点击量
                        $djcs = $dv['djcs'] - $vv['djcs'];
                        $zxcs = $dv['zxcs'] - $vv['zxcs'];
                        //如果没变化就不写入
                        if ($djcs > 0 || $zxcs > 0) {
                            $d = array(
                                'djcs' => (int)$djcs,
                                'zxcs' => (int)$zxcs,
                                'originality_id' => $vv['originality_id'],
                                'member_id' => $v,
                                'date' => $start_time
                            );
                            $res = M('datedata')->add($d);
                            //  dump($res);
                            dump(M()->_sql());
                        }
                        break;
                    }

                }

                echo '新创意';
                if (($vv['djcs'] > 0 || $vv['zxcs'] > 0) && $isnew==1 ) {
                    $d = array(
                        'djcs' => (int)$vv['djcs'],
                        'zxcs' => (int)$vv['zxcs'],
                        'originality_id' => $vv['originality_id'],
                        'member_id' => $v,
                        'date' => $start_time
                    );
                    $res = M('datedata')->add($d);
                    //  dump($res);
                    dump(M()->_sql());

                }


            }


        }


    }

}
